CS 5010: Problem Set 2

Out: Monday, September 22, 2014

Due: Monday, September 29, 2014 at 6:00pm local time

The goal of this problem set is to help you design functions that deal with the Universe Module.

Remember that you must follow the design recipe. Your deliverables include the data analysis (including template), contract and purpose header, examples, design strategy, code, and tests. You must also include your laboratory notebook.


As you did before, download a copy of extras.rkt and cat.png put them the folder with your solutions. Import the extras library by including the line

(require "extras.rkt")
at the top of your file with the other requires. Then, for each problem, put in lines that say
(provide function)
for each deliverable function. Thus, for problem 1, the top of your file should say
(require 2htdp/universe)
(require 2htdp/image)
(require rackunit)
(require "extras.rkt")

;; run with (run 0)

(provide run)
(provide initial-world)
(provide world-x)
(provide world-y)
and so on.This will allow our testing framework to import your file and do automated testing on it.

Note: For all universe programs, you may assume that the mouse is never dragged or moved outside of the canvas. Once the mouse enters the canvas, if the mouse ever leaves the canvas, then the behavior of your system is unspecified.


  1. (Draggable Rectangle) Write a universe program rectangle.rkt that displays a selectable, draggable rectangle. More specifically:

    Here's a small demo. The little rings are a video effect, and are not created by the program you are to create.

    Provide the following functions:

    run : Any -> World
    GIVEN: any value
    EFFECT: ignores its argument and starts the interactive program.
    RETURNS: the final state of the world.
    
    initial-world : Any -> World
    GIVEN: any value
    RETURNS: the initial world.
    Ignores its argument.
    
    world-x : World -> Integer
    world-y : World -> Integer
    RETURNS: the coordinates of the center of the rectangle
    NOTE: if these are part of the world struct, you don't need to
    write any deliverables for these functions.
    
    world-selected? : World -> Boolean
    GIVEN a world
    RETURNS: true iff the rectangle is selected.
    NOTE: if selected? is part of the world struct, you don't need to
    write any deliverables for this function
    
    world-after-mouse-event : World Integer Integer MouseEvent -> World
    RETURNS: the world that follows the given mouse event.
    

    For what it's worth, my solution to this problem was 268 lines, and the median time for students on this problem was between 8 and 10 hours.

    Before you turn in your solution, make sure it passes the tests in ps02-rectangle-qualification.rkt. As before, download this file, save it in your set02 directory, and run it to qualify your program for grading.

  2. (Two Bouncing Cats) Write a program two-bouncing-cats.rkt. This program should be an extension of two-draggable-cats.rkt to satisfy the following requirements:

    Here's a small demo. Turn on the sound so you can hear me say when I press an arrow key. The little rings are a video effect, and are not created by the program you are to create.

    Observe that when the user drags the cat with the mouse, the cat jumps so that it is centered on the mouse position (unlike the behavior of the rectangle in problem 1). Please leave this behavior as is; do NOT change to match the behavior of problem 1.

    Provide the following functions:

    ;; initial-world : Integer -> World
    ;; GIVEN: a y-coordinate
    ;; RETURNS: a world with two unselected cats, spaced evenly across the
    ;; canvas in the x-direction, and falling, and placed at the given y
    ;; coordinate.
    
    ;; world-after-tick : World -> World
    ;; RETURNS: the world that should follow the given world after a tick.
    
    ;; world-after-mouse-event : World Integer Integer MouseEvent -> World
    ;; RETURNS: the world that follows the given mouse event.
    
    ;; world-after-key-event : World KeyEvent -> World
    ;; RETURNS: the world that follows the given key event.
    
    ;; world-cat1 : World -> Cat
    ;; world-cat2 : World -> Cat
    ;; world-paused? : World -> Boolean
    ;; RETURNS: the specified component of the given world
    ;; NOTE: these are part of the world struct, so you don't need to
    ;; write any deliverables for these functions.
    
    ;; cat-x-pos : Cat -> Integer
    ;; cat-y-pos : Cat -> Integer
    ;; cat-selected? : Cat -> Boolean
    ;; RETURNS: the specified component of the given cat
    ;; NOTE: these are part of the cat struct, so you don't need to
    ;; write any deliverables for these functions.
    
    ;; cat-north? : Cat -> Boolean
    ;; cat-east?  : Cat -> Boolean
    ;; cat-south? : Cat -> Boolean
    ;; cat-west?  : Cat -> Boolean
    ;; GIVEN: a Cat c
    ;; RETURNS: true iff c is travelling in the specified direction.
    ;; NOTE: you will have to extend the cat struct to represent the cat's
    ;; direction, so you will need to define these functions, and provide the
    ;; deliverables for them, as usual.
    ;; NOTE: you get to design the new cat struct yourself.  However, if
    ;; you look at these functions and decide to add 4 new fields north?,
    ;; east?, south?, and west?, that is probably a bad decision.  Surely
    ;; you can do better than that.
    

    For what it's worth, my solution to this problem was 383 lines (without tests). Of these, approximately 130 lines were new compared to two-draggable-cats. Median time on task for students in recent semesters was around 11 hours.

    Qualify your program for submission by running ps02-bouncing-cats-qualification.rkt as you did for problem 1.


Last modified: Sun Sep 21 18:56:41 Eastern Daylight Time 2014